<template>
  <div><el-form ref="deptForm" :model="form" :rules="rules" label-width="120px">
    <el-form-item label="部门名称" prop="name">
      <el-input v-model="form.name" style="width:80%" placeholder="1-50个字符" />
    </el-form-item>
    <el-form-item label="部门编码" :model="form" prop="code">
      <el-input v-model="form.code" style="width:80%" placeholder="1-50个字符" />
    </el-form-item>
    <el-form-item label="部门负责人" :model="form" prop="manager">
      <el-select v-model="form.manager" style="width:80%" placeholder="请选择">
        <el-option v-for="item in Administrator" :key="item.id" :value="item.username" :label="item.username" />
      </el-select>
    </el-form-item>
    <el-form-item label="部门介绍" :model="form" prop="introduce">
      <el-input v-model="form.introduce" style="width:80%" placeholder="1-300个字符" type="textarea" :rows="3" />
    </el-form-item>
    <el-form-item>
      <el-button type="primary" size="small" @click="hSubmit">确定</el-button>
      <el-button size="small" @click="hCancel">取消</el-button>
    </el-form-item>
  </el-form></div>
</template>

<script>
import { getEmployeeSimple } from '../../api/employees'
import { addDepartments, getDepartmentDetailById, updateDepartments } from '../../api/departments'
export default {
  props: {
    id: { type: String, required: true },
    isEdit: { type: Boolean, required: false },
    originList: { type: Array, required: true }
  },
  data() {
    // 校验名字
    const validName = (rule, value, callback) => {
      let nameList = []
      if (this.isEdit) {
        const pid = this.originList.find(item => item.id === this.id).pid
        nameList = this.originList.filter(item => item.pid === pid && item.id !== this.id).map(item => item.name)
        console.log('编辑', nameList)
      } else {
        nameList = this.originList.filter(item => item.pid === this.id || item.id === this.id).map(item => item.name)
        console.log(nameList)
      }
      //  如果nameList中包含value，校验不通过
      nameList.includes(value) ? callback(new Error(value + '已经占用')) : callback()
    }
    // 校验部门编码
    const validCode = (rule, value, callback) => {
      // 验证不通过callback(new Error('错误说明'))
      // const codeList = this.originList.map(item => item.code)
      // console.log('部门编号', value, codeList)
      // if (codeList.includes(value)) {
      //   callback(new Error(value + '已经被占用'))
      // } else {
      //   //  验证通过callback()
      //   callback()
      // }
      if (this.isEdit) {
        const arr = this.originList.filter(item => item.id !== this.id)
        const codeList = arr.map(item => item.code)
        console.log('部门编号', value, codeList)
        codeList.includes(value) ? callback(new Error(value + '部门编号已经有了')) : callback()
      } else {
        const codeList = this.originList.map(item => item.code)
        console.log('部门编号', value, codeList)
        codeList.includes(value) ? callback(new Error(value + '部门编号已经有了')) : callback()
      }
    }
    return {
      form: {
        name: '', // 部门名称
        code: '', // 部门编码
        manager: '', // 部门管理者
        introduce: '' // 部门介绍
      },
      Administrator: [],

      rules: {
        name: [
          { required: true, message: '起一个部门名字吧', trigger: 'blur' },
          { min: '1', max: '12', message: '部门名为1-12位', trigger: 'blur' },
          { validator: validName, trigger: 'blur' }
        ],
        code: [
          { required: true, message: '部门编码还空着呢', trigger: 'blur' },
          { validator: validCode, trigger: 'blur' }
        ],
        manager: [
          { required: true, message: '找一个部门负责人吧', trigger: 'blur' }
        ],
        introduce: [
          { required: true, message: '部门介绍不能为空', trigger: 'blur' },
          { min: '1', max: '22', message: '简单介绍一下部门吧', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    this.loadEmployee()
    if (this.isEdit) {
      this.loadDepartmentDetailById()
    }
  },
  methods: {
    // 修改部门
    async loadDepartmentDetailById() {
      try {
        const res = await getDepartmentDetailById(this.id)
        console.log('666666666', res)
        this.form = res.data
      } catch (err) {
        console.log(err)
      }
    },
    async loadEmployee() {
      try {
        const res = await getEmployeeSimple()
        console.log('员工简单列表', res)
        this.Administrator = res.data
      } catch (err) {
        console.log(err)
      }
    },
    // 添加部门
    async doAdd() {
      // 收集参数
      const params = { ...this.form, pid: this.id }
      // console.log('收集参数', params)
      try {
        const res = await addDepartments(params)
        console.log(res)
        this.$emit('update-depart')
      } catch (err) {
        console.log(err)
      }
    },
    async doEdit() {
      // 1.提交
      try {
        // console.log('editDepartment', this.form)
        const res = await updateDepartments(this.form)
        console.log('updateDepartments...', res)

        // 2.子传父：通知父组件：关闭弹层，更新数据
        this.$emit('update-depart')
      } catch (err) {
        console.log(err)
      }
    },
    hSubmit() {
      this.$refs.form.validate(valid => {
        if (valid) {
          this.isEdit ? this.doEdit() : this.doAdd()
        }
      })
    },
    // 点击取消
    hCancel() {
      this.$emit('close')
    }

  }
}
</script>

<style lang="less" scoped>

</style>
